package redis

import (
	"gopkg.in/redis.v5"
	"fmt"
	"SUNMIGO/viper"
	"SUNMIGO/base"
)

func GetRedisOptions(db string) *redis.Options {

	host := viper.C.GetString("redisServer.host")
	post := viper.C.GetString("redisServer.port")
	auth := viper.C.GetString("redisServer.auth")
	//prefix := viper.C.GetString("redisServer.prefix")
	encryption := viper.C.GetInt("redisServer.encryption")
	dbIndex := viper.C.GetInt("redisDB." + db)

	if encryption == 1 {
		auth = base.GetMD5(auth)
	}

	options := redis.Options{
		Addr: host + post,
		Password: auth,
		DB: dbIndex,
	}

	return &options
}

func GetRedisDB(db string) (*redis.Client, error){

	options := GetRedisOptions(db)

	client := redis.NewClient(options)

	pong, err := client.Ping().Result()

	fmt.Println(pong, err)

	return client, err
}